class Solution {
public:
    int ret = 0;
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { -1, 1, 0, 0 };
    void dfs(vector<vector<int>>& grid, int i, int j, int gold)
    {
        gold += grid[i][j];
        ret = max(ret, gold);

        int tmp = grid[i][j];
        grid[i][j] = 0;

        for(int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] != 0)
                dfs(grid, x, y, gold);
        }

        grid[i][j] = tmp;
    }
    int getMaximumGold(vector<vector<int>>& grid) {
        for(int i = 0; i < grid.size(); i++)
        {
            for(int j = 0; j < grid[0].size(); j++)
            {
                dfs(grid, i, j, 0);
            }
        }
        return ret;
    }
};